package com.qys.livesMall.common.utils;

import com.qys.livesMall.common.exception.QysException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.List;

/**
 * 参数检查工具类
 * @author yxh
 */
public class ParamCheckUtil {

    public static void checkField(Object obj, Boolean isAll, String... propertys) {
        if (obj != null) {
            Class<? extends Object> clazz = obj.getClass();

            if (isAll) {
                PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(clazz);
                for (int p = 0; p < propertyDescriptors.length; p++) {
                    checkEachField(obj, propertyDescriptors[p]);
                }
            } else {
                if (propertys != null && propertys.length > 0) {
                    //遍历所有属性
                    for (int i = 0; i < propertys.length; i++) {
                        String property = propertys[i];
                        //获取属性信息
                        BeanUtils.getPropertyDescriptors(clazz);
                        PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, property);
                        checkEachField(obj, pd);
                    }
                }
            }
        }
    }

    private static void checkEachField(Object obj, PropertyDescriptor pd) {
        Class<? extends Object> clazz = obj.getClass();
        String property = pd.getName();
        if (pd != null) {
            //获取当前字段的javabean读方法
            Method readMethod = pd.getReadMethod();
            if (readMethod != null) {

                Object invoke;
                try {
                    invoke = readMethod.invoke(obj);
                } catch (Exception e) {
                    throw new QysException("方法 " + readMethod.getName() + "无法执行");
                }

                if (invoke != null) {
                    //String类型单独处理
                    Class<?> propertyType = pd.getPropertyType();
                    if ("java.lang.String".equals(propertyType.getName())) {
                        if (StringUtils.isBlank((String) invoke)) {
                            throw new QysException("错误 : [ " + property + " ] 不能为空!");
                        }
                    } else if ("java.util.List".equals(propertyType.getName())) {
                        List list = (List) invoke;
                        if (list.size() == 0) {
                            throw new QysException("错误 : [ " + property + " ] 不能为空!");
                        }
                    }
                } else {
                    throw new QysException("错误 : [ " + property + " ] 不能为空!");
                }
            } else {
                throw new QysException("在 " + clazz + "中 找不到" + "[ " + property + " ] 的 读方法");
            }
        } else {
            throw new QysException("在 " + clazz + "中 找不到" + "[ " + property + " ] 属性");
        }
    }


}
